package com.sto.stoapp.thread;

import android.content.Context;
import android.os.Handler;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.http.HttpManager;
import com.sto.stoapp.http.URLActions;
import com.sto.stoapp.http.request.LogoutResquest;
import com.sto.stoapp.http.response.LogoutResponse;
import com.sto.stoapp.log.ExceptionLog;

/**
 * 登出 注销
 * 
 * @author Administrator
 * 
 */
public class LogoutThread extends BaseThread {
	private Context context;
	/**
	 * 登出 注销类型
	 */
	public static final int LOGOUT_TYPE = 1000;

	private Handler handler;

	private boolean needDialog = false;

	/**
	 * 构造函数
	 * 
	 * @param context
	 *            上下文
	 * @param synchRequest
	 *            请求内容
	 * @param handler
	 *            传递媒介
	 */
	public LogoutThread(Context context, Handler handler, boolean needDialog) {
		super(context);
		this.context = context;
		this.handler = handler;
		this.needDialog = needDialog;
		if (needDialog)
			showDialog();
	}

	@Override
	public void run() {
		super.run();
//		Message msg = handler.obtainMessage();
//		msg.what = LOGOUT_TYPE;
		LogoutResquest request = new LogoutResquest(context);
		HttpManager mgr = new HttpManager();
		String result = mgr.doPostClient(request.buildToRequestString(),
				StoFilePreference.getInstance(context).getServerIp()
						+ URLActions.LOGOUT_ACTION);
		Gson gs = new Gson();
		LogoutResponse response = null;
		try {
			response = gs.fromJson(result, new TypeToken<LogoutResponse>() {
			}.getType());
		} catch (JsonSyntaxException e) {
			ExceptionLog.writeLog("LogoutThread", " 解析失败:result");
			e.printStackTrace();
		}
		if(response!=null){
			if("000".equals(response.respCode)){
				StoFilePreference.getInstance(context).setSessionId("");
			}
		}
//		msg.obj = response;
//		handler.sendMessage(msg);
		if (needDialog)
			cancelDialog();
	}

}
